{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<a id='HOME'></a>\n",
    "# CHAPTER 10 Systems\n",
    "## 系統\n",
    "\n",
    "* [10.1 檔案](#Files)\n",
    "* [10.2 目錄](#Directories)\n",
    "* [10.3 程序和進程](#ProgramsProcesses)\n",
    "* [10.4 日期與時間](#CalendarsClocks)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Files'></a>\n",
    "## 10.1 檔案\n",
    "[回目錄](#HOME)\n",
    "\n",
    "|語法               |功能    \t\t\t\t|使用                               |回傳   |\n",
    "|-------------------|-----------------------|-----------------------------------|-------|\n",
    "|open()\t\t\t\t|創建檔案\t\t\t\t|fout = open('oops.txt', 'wt')      |Object |\n",
    "|exists()\t\t\t|檢查檔案是否存在\t\t|os.path.exists('oops.txt')         |Boolean|\n",
    "|isfile()\t\t\t|檢查是否為檔案\t\t\t|os.path.isfile('oops.txt')         |Boolean|\n",
    "|copy()\t\t\t\t|複製檔案\t\t\t\t|shutil.copy('oops.txt', 'ohno.txt')||\n",
    "|rename()\t\t\t|重命名檔案\t\t\t\t|os.rename('ohno.txt', 'ohwell.txt')||\n",
    "|link()或者symlink()|創建鏈接(for Unix)\t\t|os.link('oops.txt', 'yikes.txt')   ||\n",
    "|chmod()\t\t\t|修改權限(for Unix)\t\t|os.chmod('oops.txt', 0o400)        ||\n",
    "|chown()\t\t\t|修改所有者(for Unix)\t|os.chown('oops', uid, gid)         ||\n",
    "|abspath()\t\t\t|獲取路徑名\t\t\t\t|os.path.abspath('oops.txt')        |String|\n",
    "|realpath()\t\t\t|獲取真實的路徑名\t\t|os.path.realpath('jeepers.txt')    ||\n",
    "|remove()\t\t\t|刪除檔案\t\t\t\t|os.remove('oops.txt')              ||"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='Directories'></a>\n",
    "## 10.2 目錄\n",
    "[回目錄](#HOME)\n",
    "\n",
    "|語法       |功能    \t\t|使用               |回傳   |\n",
    "|-----------|---------------|-------------------|-------|\n",
    "|mkdir()\t|創建目錄\t\t|os.mkdir('poems')  ||\n",
    "|rmdir()\t|刪除目錄       |os.rmdir('poems')\t||\n",
    "|isdir()    |檢查是否為資料夾|os.path.isdir('oops')|Boolean| \n",
    "|listdir()\t|列出目錄內容\t|os.listdir('poems')\t||\n",
    "|chdir()   \t|修改當前目錄   |os.chdir('poems')\t|| \n",
    "|glob()\t\t|列出匹配文件\t|glob.glob('m*')\t|List|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='ProgramsProcesses'></a>\n",
    "## 10.3 程序和進程\n",
    "[回目錄](#HOME)\n",
    "\n",
    "可以在python中使用終端機指令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用subprocess創建進程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Windows IP 設定\n",
      "\n",
      "   主機名稱 . . . . . . . . . . . . .: Shih-Chi\n",
      "   主要 DNS 尾碼  . . . . . . . . . .: \n",
      "   節點類型 . . . . . . . . . . . . .: 混合式\n",
      "   IP 路由啟用 . . . . . . . . . . . : 否\n",
      "   WINS Proxy 啟用 . . . . . . . . . : 否\n",
      "\n",
      "乙太網路卡 乙太網路:\n",
      "\n",
      "   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\n",
      "   連線特定 DNS 尾碼 . . . . . . . . : \n",
      "   描述 . . . . . . . . . . . . . . .: Realtek PCIe GBE Family Controller\n",
      "   實體位址 . . . . . . . . . . . . .: F0-76-1C-CC-F6-D5\n",
      "   DHCP 已啟用 . . . . . . . . . . . : 是\n",
      "   自動設定啟用 . . . . . . . . . . .: 是\n",
      "\n",
      "無線區域網路介面卡 區域連線* 2:\n",
      "\n",
      "   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\n",
      "   連線特定 DNS 尾碼 . . . . . . . . : \n",
      "   描述 . . . . . . . . . . . . . . .: Microsoft Wi-Fi Direct Virtual Adapter\n",
      "   實體位址 . . . . . . . . . . . . .: 12-6D-C7-36-2C-5F\n",
      "   DHCP 已啟用 . . . . . . . . . . . : 是\n",
      "   自動設定啟用 . . . . . . . . . . .: 是\n",
      "\n",
      "無線區域網路介面卡 區域連線* 4:\n",
      "\n",
      "   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\n",
      "   連線特定 DNS 尾碼 . . . . . . . . : \n",
      "   描述 . . . . . . . . . . . . . . .: Microsoft Hosted Network Virtual Adapter\n",
      "   實體位址 . . . . . . . . . . . . .: 52-6D-C7-36-2C-5F\n",
      "   DHCP 已啟用 . . . . . . . . . . . : 是\n",
      "   自動設定啟用 . . . . . . . . . . .: 是\n",
      "\n",
      "無線區域網路介面卡 Wi-Fi:\n",
      "\n",
      "   連線特定 DNS 尾碼 . . . . . . . . : \n",
      "   描述 . . . . . . . . . . . . . . .: Qualcomm Atheros AR956x Wireless Network Adapter\n",
      "   實體位址 . . . . . . . . . . . . .: 60-6D-C7-36-2C-5F\n",
      "   DHCP 已啟用 . . . . . . . . . . . : 是\n",
      "   自動設定啟用 . . . . . . . . . . .: 是\n",
      "   連結-本機 IPv6 位址 . . . . . . . : fe80::f860:ac39:cd7d:cea6%8(偏好選項) \n",
      "   IPv4 位址 . . . . . . . . . . . . : 192.168.0.107(偏好選項) \n",
      "   子網路遮罩 . . . . . . . . . . . .: 255.255.255.0\n",
      "   租用取得 . . . . . . . . . . . . .: 2016年9月20日 下午 09:17:58\n",
      "   租用到期 . . . . . . . . . . . . .: 2016年9月21日 下午 09:17:58\n",
      "   預設閘道 . . . . . . . . . . . . .: 192.168.0.1\n",
      "   DHCP 伺服器 . . . . . . . . . . . : 192.168.0.1\n",
      "   DHCPv6 IAID . . . . . . . . . . . : 90205639\n",
      "   DHCPv6 用戶端 DUID. . . . . . . . : 00-01-00-01-1E-36-8D-B1-F0-76-1C-CC-F6-D5\n",
      "   DNS 伺服器 . . . . . . . . . . . .: 192.168.0.1\n",
      "   NetBIOS over Tcpip . . . . . . . .: 啟用\n",
      "\n",
      "通道介面卡 區域連線* 3:\n",
      "\n",
      "   連線特定 DNS 尾碼 . . . . . . . . : \n",
      "   描述 . . . . . . . . . . . . . . .: Microsoft Teredo Tunneling Adapter\n",
      "   實體位址 . . . . . . . . . . . . .: 00-00-00-00-00-00-00-E0\n",
      "   DHCP 已啟用 . . . . . . . . . . . : 否\n",
      "   自動設定啟用 . . . . . . . . . . .: 是\n",
      "   IPv6 位址. . . . . . . . . . . . .: 2001:0:9d38:6abd:18fb:3f55:3f57:ff94(偏好選項) \n",
      "   連結-本機 IPv6 位址 . . . . . . . : fe80::18fb:3f55:3f57:ff94%7(偏好選項) \n",
      "   預設閘道 . . . . . . . . . . . . .: ::\n",
      "   DHCPv6 IAID . . . . . . . . . . . : 117440512\n",
      "   DHCPv6 用戶端 DUID. . . . . . . . : 00-01-00-01-1E-36-8D-B1-F0-76-1C-CC-F6-D5\n",
      "   NetBIOS over Tcpip . . . . . . . .: 停用\n",
      "\n",
      "通道介面卡 isatap.{94742C03-F6F8-4338-BD44-BDA0C15DBFC3}:\n",
      "\n",
      "   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\n",
      "   連線特定 DNS 尾碼 . . . . . . . . : \n",
      "   描述 . . . . . . . . . . . . . . .: Microsoft ISATAP Adapter\n",
      "   實體位址 . . . . . . . . . . . . .: 00-00-00-00-00-00-00-E0\n",
      "   DHCP 已啟用 . . . . . . . . . . . : 否\n",
      "   自動設定啟用 . . . . . . . . . . .: 是\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "==============================================================\n",
      "b'\\r\\nWindows IP \\xb3]\\xa9w\\r\\n\\r\\n   \\xa5D\\xbe\\xf7\\xa6W\\xba\\xd9 . . . . . . . . . . . . .: Shih-Chi\\r\\n   \\xa5D\\xadn DNS \\xa7\\xc0\\xbdX  . . . . . . . . . .: \\r\\n   \\xb8`\\xc2I\\xc3\\xfe\\xab\\xac . . . . . . . . . . . . .: \\xb2V\\xa6X\\xa6\\xa1\\r\\n   IP \\xb8\\xf4\\xa5\\xd1\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xa7_\\r\\n   WINS Proxy \\xb1\\xd2\\xa5\\xce . . . . . . . . . : \\xa7_\\r\\n\\r\\n\\xa4A\\xa4\\xd3\\xba\\xf4\\xb8\\xf4\\xa5d \\xa4A\\xa4\\xd3\\xba\\xf4\\xb8\\xf4:\\r\\n\\r\\n   \\xb4C\\xc5\\xe9\\xaa\\xac\\xbaA . . . . . . . . . . . . .: \\xb4C\\xc5\\xe9\\xa4w\\xa4\\xa4\\xc2_\\xb3s\\xbdu\\r\\n   \\xb3s\\xbdu\\xafS\\xa9w DNS \\xa7\\xc0\\xbdX . . . . . . . . : \\r\\n   \\xb4y\\xadz . . . . . . . . . . . . . . .: Realtek PCIe GBE Family Controller\\r\\n   \\xb9\\xea\\xc5\\xe9\\xa6\\xec\\xa7} . . . . . . . . . . . . .: F0-76-1C-CC-F6-D5\\r\\n   DHCP \\xa4w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xacO\\r\\n   \\xa6\\xdb\\xb0\\xca\\xb3]\\xa9w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . .: \\xacO\\r\\n\\r\\n\\xb5L\\xbdu\\xb0\\xcf\\xb0\\xec\\xba\\xf4\\xb8\\xf4\\xa4\\xb6\\xad\\xb1\\xa5d \\xb0\\xcf\\xb0\\xec\\xb3s\\xbdu* 2:\\r\\n\\r\\n   \\xb4C\\xc5\\xe9\\xaa\\xac\\xbaA . . . . . . . . . . . . .: \\xb4C\\xc5\\xe9\\xa4w\\xa4\\xa4\\xc2_\\xb3s\\xbdu\\r\\n   \\xb3s\\xbdu\\xafS\\xa9w DNS \\xa7\\xc0\\xbdX . . . . . . . . : \\r\\n   \\xb4y\\xadz . . . . . . . . . . . . . . .: Microsoft Wi-Fi Direct Virtual Adapter\\r\\n   \\xb9\\xea\\xc5\\xe9\\xa6\\xec\\xa7} . . . . . . . . . . . . .: 12-6D-C7-36-2C-5F\\r\\n   DHCP \\xa4w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xacO\\r\\n   \\xa6\\xdb\\xb0\\xca\\xb3]\\xa9w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . .: \\xacO\\r\\n\\r\\n\\xb5L\\xbdu\\xb0\\xcf\\xb0\\xec\\xba\\xf4\\xb8\\xf4\\xa4\\xb6\\xad\\xb1\\xa5d \\xb0\\xcf\\xb0\\xec\\xb3s\\xbdu* 4:\\r\\n\\r\\n   \\xb4C\\xc5\\xe9\\xaa\\xac\\xbaA . . . . . . . . . . . . .: \\xb4C\\xc5\\xe9\\xa4w\\xa4\\xa4\\xc2_\\xb3s\\xbdu\\r\\n   \\xb3s\\xbdu\\xafS\\xa9w DNS \\xa7\\xc0\\xbdX . . . . . . . . : \\r\\n   \\xb4y\\xadz . . . . . . . . . . . . . . .: Microsoft Hosted Network Virtual Adapter\\r\\n   \\xb9\\xea\\xc5\\xe9\\xa6\\xec\\xa7} . . . . . . . . . . . . .: 52-6D-C7-36-2C-5F\\r\\n   DHCP \\xa4w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xacO\\r\\n   \\xa6\\xdb\\xb0\\xca\\xb3]\\xa9w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . .: \\xacO\\r\\n\\r\\n\\xb5L\\xbdu\\xb0\\xcf\\xb0\\xec\\xba\\xf4\\xb8\\xf4\\xa4\\xb6\\xad\\xb1\\xa5d Wi-Fi:\\r\\n\\r\\n   \\xb3s\\xbdu\\xafS\\xa9w DNS \\xa7\\xc0\\xbdX . . . . . . . . : \\r\\n   \\xb4y\\xadz . . . . . . . . . . . . . . .: Qualcomm Atheros AR956x Wireless Network Adapter\\r\\n   \\xb9\\xea\\xc5\\xe9\\xa6\\xec\\xa7} . . . . . . . . . . . . .: 60-6D-C7-36-2C-5F\\r\\n   DHCP \\xa4w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xacO\\r\\n   \\xa6\\xdb\\xb0\\xca\\xb3]\\xa9w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . .: \\xacO\\r\\n   \\xb3s\\xb5\\xb2-\\xa5\\xbb\\xbe\\xf7 IPv6 \\xa6\\xec\\xa7} . . . . . . . : fe80::f860:ac39:cd7d:cea6%8(\\xb0\\xbe\\xa6n\\xbf\\xef\\xb6\\xb5) \\r\\n   IPv4 \\xa6\\xec\\xa7} . . . . . . . . . . . . : 192.168.0.107(\\xb0\\xbe\\xa6n\\xbf\\xef\\xb6\\xb5) \\r\\n   \\xa4l\\xba\\xf4\\xb8\\xf4\\xbeB\\xb8n . . . . . . . . . . . .: 255.255.255.0\\r\\n   \\xaf\\xb2\\xa5\\xce\\xa8\\xfa\\xb1o . . . . . . . . . . . . .: 2016\\xa6~9\\xa4\\xeb20\\xa4\\xe9 \\xa4U\\xa4\\xc8 09:17:58\\r\\n   \\xaf\\xb2\\xa5\\xce\\xa8\\xec\\xb4\\xc1 . . . . . . . . . . . . .: 2016\\xa6~9\\xa4\\xeb21\\xa4\\xe9 \\xa4U\\xa4\\xc8 09:17:58\\r\\n   \\xb9w\\xb3]\\xb9h\\xb9D . . . . . . . . . . . . .: 192.168.0.1\\r\\n   DHCP \\xa6\\xf8\\xaaA\\xbe\\xb9 . . . . . . . . . . . : 192.168.0.1\\r\\n   DHCPv6 IAID . . . . . . . . . . . : 90205639\\r\\n   DHCPv6 \\xa5\\xce\\xa4\\xe1\\xba\\xdd DUID. . . . . . . . : 00-01-00-01-1E-36-8D-B1-F0-76-1C-CC-F6-D5\\r\\n   DNS \\xa6\\xf8\\xaaA\\xbe\\xb9 . . . . . . . . . . . .: 192.168.0.1\\r\\n   NetBIOS over Tcpip . . . . . . . .: \\xb1\\xd2\\xa5\\xce\\r\\n\\r\\n\\xb3q\\xb9D\\xa4\\xb6\\xad\\xb1\\xa5d \\xb0\\xcf\\xb0\\xec\\xb3s\\xbdu* 3:\\r\\n\\r\\n   \\xb3s\\xbdu\\xafS\\xa9w DNS \\xa7\\xc0\\xbdX . . . . . . . . : \\r\\n   \\xb4y\\xadz . . . . . . . . . . . . . . .: Microsoft Teredo Tunneling Adapter\\r\\n   \\xb9\\xea\\xc5\\xe9\\xa6\\xec\\xa7} . . . . . . . . . . . . .: 00-00-00-00-00-00-00-E0\\r\\n   DHCP \\xa4w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xa7_\\r\\n   \\xa6\\xdb\\xb0\\xca\\xb3]\\xa9w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . .: \\xacO\\r\\n   IPv6 \\xa6\\xec\\xa7}. . . . . . . . . . . . .: 2001:0:9d38:6abd:18fb:3f55:3f57:ff94(\\xb0\\xbe\\xa6n\\xbf\\xef\\xb6\\xb5) \\r\\n   \\xb3s\\xb5\\xb2-\\xa5\\xbb\\xbe\\xf7 IPv6 \\xa6\\xec\\xa7} . . . . . . . : fe80::18fb:3f55:3f57:ff94%7(\\xb0\\xbe\\xa6n\\xbf\\xef\\xb6\\xb5) \\r\\n   \\xb9w\\xb3]\\xb9h\\xb9D . . . . . . . . . . . . .: ::\\r\\n   DHCPv6 IAID . . . . . . . . . . . : 117440512\\r\\n   DHCPv6 \\xa5\\xce\\xa4\\xe1\\xba\\xdd DUID. . . . . . . . : 00-01-00-01-1E-36-8D-B1-F0-76-1C-CC-F6-D5\\r\\n   NetBIOS over Tcpip . . . . . . . .: \\xb0\\xb1\\xa5\\xce\\r\\n\\r\\n\\xb3q\\xb9D\\xa4\\xb6\\xad\\xb1\\xa5d isatap.{94742C03-F6F8-4338-BD44-BDA0C15DBFC3}:\\r\\n\\r\\n   \\xb4C\\xc5\\xe9\\xaa\\xac\\xbaA . . . . . . . . . . . . .: \\xb4C\\xc5\\xe9\\xa4w\\xa4\\xa4\\xc2_\\xb3s\\xbdu\\r\\n   \\xb3s\\xbdu\\xafS\\xa9w DNS \\xa7\\xc0\\xbdX . . . . . . . . : \\r\\n   \\xb4y\\xadz . . . . . . . . . . . . . . .: Microsoft ISATAP Adapter\\r\\n   \\xb9\\xea\\xc5\\xe9\\xa6\\xec\\xa7} . . . . . . . . . . . . .: 00-00-00-00-00-00-00-E0\\r\\n   DHCP \\xa4w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . . : \\xa7_\\r\\n   \\xa6\\xdb\\xb0\\xca\\xb3]\\xa9w\\xb1\\xd2\\xa5\\xce . . . . . . . . . . .: \\xacO\\r\\n'\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "==============================================================\n",
      "(0, '\\nWindows IP 設定\\n\\n   主機名稱 . . . . . . . . . . . . .: Shih-Chi\\n   主要 DNS 尾碼  . . . . . . . . . .: \\n   節點類型 . . . . . . . . . . . . .: 混合式\\n   IP 路由啟用 . . . . . . . . . . . : 否\\n   WINS Proxy 啟用 . . . . . . . . . : 否\\n\\n乙太網路卡 乙太網路:\\n\\n   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\\n   連線特定 DNS 尾碼 . . . . . . . . : \\n   描述 . . . . . . . . . . . . . . .: Realtek PCIe GBE Family Controller\\n   實體位址 . . . . . . . . . . . . .: F0-76-1C-CC-F6-D5\\n   DHCP 已啟用 . . . . . . . . . . . : 是\\n   自動設定啟用 . . . . . . . . . . .: 是\\n\\n無線區域網路介面卡 區域連線* 2:\\n\\n   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\\n   連線特定 DNS 尾碼 . . . . . . . . : \\n   描述 . . . . . . . . . . . . . . .: Microsoft Wi-Fi Direct Virtual Adapter\\n   實體位址 . . . . . . . . . . . . .: 12-6D-C7-36-2C-5F\\n   DHCP 已啟用 . . . . . . . . . . . : 是\\n   自動設定啟用 . . . . . . . . . . .: 是\\n\\n無線區域網路介面卡 區域連線* 4:\\n\\n   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\\n   連線特定 DNS 尾碼 . . . . . . . . : \\n   描述 . . . . . . . . . . . . . . .: Microsoft Hosted Network Virtual Adapter\\n   實體位址 . . . . . . . . . . . . .: 52-6D-C7-36-2C-5F\\n   DHCP 已啟用 . . . . . . . . . . . : 是\\n   自動設定啟用 . . . . . . . . . . .: 是\\n\\n無線區域網路介面卡 Wi-Fi:\\n\\n   連線特定 DNS 尾碼 . . . . . . . . : \\n   描述 . . . . . . . . . . . . . . .: Qualcomm Atheros AR956x Wireless Network Adapter\\n   實體位址 . . . . . . . . . . . . .: 60-6D-C7-36-2C-5F\\n   DHCP 已啟用 . . . . . . . . . . . : 是\\n   自動設定啟用 . . . . . . . . . . .: 是\\n   連結-本機 IPv6 位址 . . . . . . . : fe80::f860:ac39:cd7d:cea6%8(偏好選項) \\n   IPv4 位址 . . . . . . . . . . . . : 192.168.0.107(偏好選項) \\n   子網路遮罩 . . . . . . . . . . . .: 255.255.255.0\\n   租用取得 . . . . . . . . . . . . .: 2016年9月20日 下午 09:17:58\\n   租用到期 . . . . . . . . . . . . .: 2016年9月21日 下午 09:17:58\\n   預設閘道 . . . . . . . . . . . . .: 192.168.0.1\\n   DHCP 伺服器 . . . . . . . . . . . : 192.168.0.1\\n   DHCPv6 IAID . . . . . . . . . . . : 90205639\\n   DHCPv6 用戶端 DUID. . . . . . . . : 00-01-00-01-1E-36-8D-B1-F0-76-1C-CC-F6-D5\\n   DNS 伺服器 . . . . . . . . . . . .: 192.168.0.1\\n   NetBIOS over Tcpip . . . . . . . .: 啟用\\n\\n通道介面卡 區域連線* 3:\\n\\n   連線特定 DNS 尾碼 . . . . . . . . : \\n   描述 . . . . . . . . . . . . . . .: Microsoft Teredo Tunneling Adapter\\n   實體位址 . . . . . . . . . . . . .: 00-00-00-00-00-00-00-E0\\n   DHCP 已啟用 . . . . . . . . . . . : 否\\n   自動設定啟用 . . . . . . . . . . .: 是\\n   IPv6 位址. . . . . . . . . . . . .: 2001:0:9d38:6abd:18fb:3f55:3f57:ff94(偏好選項) \\n   連結-本機 IPv6 位址 . . . . . . . : fe80::18fb:3f55:3f57:ff94%7(偏好選項) \\n   預設閘道 . . . . . . . . . . . . .: ::\\n   DHCPv6 IAID . . . . . . . . . . . : 117440512\\n   DHCPv6 用戶端 DUID. . . . . . . . : 00-01-00-01-1E-36-8D-B1-F0-76-1C-CC-F6-D5\\n   NetBIOS over Tcpip . . . . . . . .: 停用\\n\\n通道介面卡 isatap.{94742C03-F6F8-4338-BD44-BDA0C15DBFC3}:\\n\\n   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線\\n   連線特定 DNS 尾碼 . . . . . . . . : \\n   描述 . . . . . . . . . . . . . . .: Microsoft ISATAP Adapter\\n   實體位址 . . . . . . . . . . . . .: 00-00-00-00-00-00-00-E0\\n   DHCP 已啟用 . . . . . . . . . . . : 否\\n   自動設定啟用 . . . . . . . . . . .: 是')\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "==============================================================\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "import subprocess\n",
    "ret = subprocess.getoutput('ipconfig /all')\n",
    "print(ret)\n",
    "\n",
    "print('\\n\\n\\n\\n\\n==============================================================')\n",
    "ret = subprocess.check_output(['ipconfig', '/all'])\n",
    "print(ret)\n",
    "\n",
    "print('\\n\\n\\n\\n\\n==============================================================')\n",
    "ret = subprocess.getstatusoutput('ipconfig /all')\n",
    "print(ret)\n",
    "\n",
    "print('\\n\\n\\n\\n\\n==============================================================')\n",
    "ret = subprocess.call('ipconfig /all')\n",
    "print(ret)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用multiprocessing創建進程\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "import multiprocessing\n",
    "import os\n",
    "\n",
    "def do_this(what):\n",
    "    whoami(what)\n",
    "    \n",
    "def whoami(what):\n",
    "    print(\"Process %s says: %s\" % (os.getpid(), what))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    whoami(\"I'm the main program\")\n",
    "    for n in range(4):\n",
    "        p = multiprocessing.Process(target=do_this,args=(\"I'm function %s\" % n,))\n",
    "        p.start()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Process 4560 says: I'm the main program\n",
      "Process 8348 says: I'm function 0\n",
      "Process 1976 says: I'm function 1\n",
      "Process 7904 says: I'm function 3\n",
      "Process 1100 says: I'm function 2\n"
     ]
    }
   ],
   "source": [
    "print(subprocess.getoutput('python Data/mp.py'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用terminate()終止進程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm main, in process 5180\n"
     ]
    }
   ],
   "source": [
    "print(subprocess.getoutput('python Data/mp2.py'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<a id='CalendarsClocks'></a>\n",
    "## 10.4 日期與時間\n",
    "[回目錄](#HOME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "\n",
    "# 閏年判定\n",
    "print(calendar.isleap(1900))\n",
    "print(calendar.isleap(1996))\n",
    "print(calendar.isleap(1999))\n",
    "print(calendar.isleap(2000))\n",
    "print(calendar.isleap(2002))\n",
    "print(calendar.isleap(2004))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2014-10-31\n",
      "31\n",
      "10\n",
      "2014\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2014-10-31'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 日期函數\n",
    "\n",
    "from datetime import date\n",
    "halloween = date(2014, 10, 31)\n",
    "print(halloween)\n",
    "\n",
    "print(halloween.day)\n",
    "print(halloween.month)\n",
    "print(halloween.year)\n",
    "\n",
    "halloween.isoformat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2016, 9, 20)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 叫出今天\n",
    "\n",
    "from datetime import date\n",
    "now = date.today()\n",
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-09-21\n",
      "2016-10-07\n",
      "2016-09-19\n"
     ]
    }
   ],
   "source": [
    "# 日期加減\n",
    "# 範圍只有 date.min（ 年 = 1， 月 = 1， 日 = 1） 到 date.max（ 年 = 9999， 月 = 12，日 = 31）。\n",
    "# 因此，不能使用它來進行和歷史或者天文相關的計算。\n",
    "\n",
    "from datetime import timedelta\n",
    "one_day = timedelta(days=1)\n",
    "tomorrow = now + one_day\n",
    "print(tomorrow)\n",
    "\n",
    "print(now + 17*one_day)\n",
    "print(now - one_day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12:00:00\n",
      "0\n",
      "0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 時間函數\n",
    "\n",
    "from datetime import time\n",
    "noon = time(12, 0, 0)\n",
    "\n",
    "print(noon)\n",
    "\n",
    "print(noon.minute)\n",
    "print(noon.second)\n",
    "print(noon.microsecond)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2014-01-02 03:04:05.000006\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2014-01-02T03:04:05.000006'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 年 月 日 時 分 秒 微秒\n",
    "\n",
    "from datetime import datetime\n",
    "some_day = datetime(2014, 1, 2, 3, 4, 5, 6)\n",
    "print(some_day)\n",
    "\n",
    "some_day.isoformat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-09-20 22:40:56.185072\n",
      "9\n",
      "20\n",
      "22\n",
      "40\n",
      "56\n",
      "185072\n"
     ]
    }
   ],
   "source": [
    "# 獲取現在時間\n",
    "\n",
    "from datetime import datetime\n",
    "now = datetime.now()\n",
    "print(now)\n",
    "\n",
    "print(now.month)\n",
    "print(now.day)\n",
    "print(now.hour)\n",
    "print(now.minute)\n",
    "print(now.second)\n",
    "print(now.microsecond)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-09-20 12:00:00\n",
      "2016-09-20\n",
      "12:00:00\n"
     ]
    }
   ],
   "source": [
    "# 組合日期與時間\n",
    "\n",
    "from datetime import datetime, time, date\n",
    "noon = time(12)\n",
    "this_day = date.today()\n",
    "noon_today = datetime.combine(this_day, noon)\n",
    "print(noon_today)\n",
    "\n",
    "# 取出日期\n",
    "print(noon_today.date())\n",
    "# 取出時間\n",
    "print(noon_today.time())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用time模組"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1474382456.2160954\n",
      "Tue Sep 20 22:40:56 2016\n",
      "time.struct_time(tm_year=2016, tm_mon=9, tm_mday=20, tm_hour=22, tm_min=40, tm_sec=56, tm_wday=1, tm_yday=264, tm_isdst=0)\n",
      "1474382456.0\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "# 從1970/01/01累積至今的秒數\n",
    "now = time.time()\n",
    "tm = time.ctime(now)\n",
    "\n",
    "print(now)\n",
    "print(time.ctime(now)) #轉成可讀時間\n",
    "\n",
    "tm = time.localtime(now)\n",
    "print(tm)\n",
    "print(time.mktime(tm))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 讀寫日期與時間\n",
    "\n",
    "|格式化字符串 |日期/時間單元| 範圍|\n",
    "|---|---|\n",
    "|%Y |年      |1900-...|\n",
    "|%m |月      |01-12|\n",
    "|%B |月名    |January,...|\n",
    "|%b |月名縮寫|Jan,...|\n",
    "|%d |日      |01-31|\n",
    "|%A |星期    |Sunday,...|\n",
    "|%a |星期縮寫 |Sun,...|\n",
    "|%H |時（ 24 小時制）|00-23|\n",
    "|%I |時（ 12 小時制）|01-12|\n",
    "|%p |上午 / 下午 |AM, PM|\n",
    "|%M |分 |00-59|\n",
    "|%S |秒 |00-59|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time.struct_time(tm_year=2016, tm_mon=9, tm_mday=20, tm_hour=22, tm_min=40, tm_sec=56, tm_wday=1, tm_yday=264, tm_isdst=0)\n",
      "It's Tuesday, September 20, 2016, local time 10:40:56PM\n",
      "=======================================\n",
      "It's July 04, 2014, local time 12:00:00AM\n",
      "=======================================\n",
      "It's January 01, 1900, local time 10:35:00AM\n"
     ]
    }
   ],
   "source": [
    "# 日期轉文字\n",
    "\n",
    "import time\n",
    "fmt = \"It's %A, %B %d, %Y, local time %I:%M:%S%p\"\n",
    "t = time.localtime()\n",
    "print(t)\n",
    "print(time.strftime(fmt, t))\n",
    "\n",
    "# 如果使用 date 對象的 strftime() 函數，只能獲取日期部分，時間默認是午夜：\n",
    "print('=======================================')\n",
    "from datetime import date\n",
    "some_day = date(2014, 7, 4)\n",
    "fmt = \"It's %B %d, %Y, local time %I:%M:%S%p\"\n",
    "print(some_day.strftime(fmt))\n",
    "\n",
    "# 只有時間的話日期部分則為預設值\n",
    "print('=======================================')\n",
    "from datetime import time\n",
    "some_time = time(10, 35)\n",
    "print(some_time.strftime(fmt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2012, tm_mon=1, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=29, tm_isdst=-1)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文字轉日期\n",
    "\n",
    "import time\n",
    "fmt = \"%Y-%m-%d\"\n",
    "time.strptime(\"2012-01-29\", fmt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 其他模組\n",
    "\n",
    "* arrow（ http://crsmithdev.com/arrow/）\n",
    "這個模組有許多日期和時間函數，並提供了簡單易用的 API。\n",
    "\n",
    "* dateutil（ http://labix.org/python-dateutil）\n",
    "這個模組可以解析絕大多數日期格式，並且可以很好地處理相對日期和時間。\n",
    "* iso8601（ https://pypi.python.org/pypi/iso8601）\n",
    "這個模組會完善標準庫中對於 ISO8601 格式的處理。\n",
    "* fleming（ https://github.com/ambitioninc/fleming）\n",
    "這個模組提供了許多時區函數。"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
